#import
from flask import Flask,request
from flask import render_template
# 调用自定义模块中的函数
from resserch import reserch4letters
# Flask:中央登记中心
app = Flask(__name__)

# 【Flask】(https://dormousehole.readthedocs.io/en/latest/quickstart.html#id10)
# 【HTMl】(https://www.w3school.com.cn/html/index.asp)
# 【jinja2】(https://palletsprojects.com/p/jinja/)

# 1. 路由：相对路径，决定了 页面资源 的路径，如果没有登记，将返回404 not found
# 1.1 url: 相对于：http://127.0.0.1：5000的相对路径
# 1.2 methods:【GET】【POST】
# 2. 视图函数：
# 2.1 函数过程决定了你操作的方法（实现的功能），例如 if elif else for while ...
# 2.2 return：返回数据内容，text文本，HTML文件（模板渲染），JSON文件等

# 【GET】


@app.route('/', methods=['GET'])
def hello_world():
    return render_template('hello.html')


@app.route('/index', methods=['GET'])
def index():
    return render_template('index.html')

# 【POST】实现用户输入，点击提交，将数据提交到 /result


@app.route('/result', methods=['POST'])
def result():
    # 接收数据：request.form[HTML-参数名称]
    # 1. 名字的全称（完整的应用）：用户输入数据--->后端接收数据--->后端处理数据--->返回到HTML页面
    # 2. 元音字母查询（第二个功能）
    # 3. 自选：购物车 或者 课程表
    py_firstname = request.form['firstname']
    py_lastname = request.form['lastname']
    py_fullname = py_firstname.title()+' '+py_lastname.title()
    # 在 中央登记中心，写这么多 python过程的代码 是不是有点不好玩？
    # 使用模块来写函数过程，在视图函数中调用模块实现函数方法
    py_word = request.form['word']
    py_vowels = request.form['vowels']
    py_found = reserch4letters(py_vowels, py_word)

    return render_template('result.html',
                           fullname=py_fullname,
                           found=py_found)


@app.route('/name', methods=['GET'])
def html_name():
    return render_template('name.html', fullname='')


@app.route('/fullname', methods=['POST'])
def get_fullname():
    xing = request.form['xing']
    ming = request.form['ming']
    fullname = xing+' '+ming
    return render_template('name.html', fullname=fullname)


@app.route('/word', methods=['GET'])
def html_word():
    return render_template('word.html', found='')


@app.route('/found', methods=['POST'])
def get_found():
    py_word = request.form['word']
    py_vowels = request.form['vowels']
    py_found = reserch4letters(py_vowels, py_word)
    return render_template('word.html', found=py_found)


def all_good():
    good = [
        {
            'good_id': 1,
            'image': 'https://gw.alicdn.com/imgextra/i1/TB1I34QAkT2gK0jSZFkSuwIQFXa.jpg',
            'name': '镇竿阿牛血粑',
            'price': 25
        },
        {
            'good_id': 2,
            'image': 'http://union-etao.aliyuncs.com/i1/aa1a16f0-2d6a-4375-85d0-d390c4d9fa995GUO.jpg?Expires=2240463203&OSSAccessKeyId=LTAIm1ERwX8zoZUa&Signature=7mNHcklTOqKedDHjFaCXTwOQA6g%3D',
            'name': '黑尚莓树莓酒',
            'price': 298
        },
        {
            'good_id': 3,
            'image': 'https://gw.alicdn.com/imgextra/i1/TB1SPKeiQY2gK0jSZFgSuw5OFXa.jpg_q100.jpg',
            'name': '不倒翁玩具',
            'price': 18.5
        },
        {
            'good_id': 4,
            'image': 'http://union-etao.aliyuncs.com/i1/30090151-187a-4400-bc53-446b0e18932a1.jpg?Expires=2263127714&OSSAccessKeyId=LTAIm1ERwX8zoZUa&Signature=VMSqY%2FeGGXHICxdX4X6fqb%2Bc6WA%3D',
            'name': '香辣风味豆鼓酱',
            'price': 29.9
        },

    ]
    return good


@app.route('/cart', methods=['GET'])
def html_cart():
    good = all_good()
    return render_template('cart.html', good=good, result='')


@app.route('/jiesuan',methods=["POST"])
def get_jiesuan():

    good = all_good()
    total_price=0
    for g in good:
        quantity=request.form[str(g['good_id'])]
        quantity=int(quantity)
        price = g['price'] * quantity
        total_price=total_price+price

    return render_template('cart.html', good='', result=total_price)





if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5000, debug=True)
